package yxy.game.pm2.module.avatar;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.joda.time.DateTime;
import org.redisson.api.RMap;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.headframe;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

public class HeadframeModule extends BaseGameRedisLockBean {

	static final public int DefaultHeadFrame = 101;

	static protected enum HeadframeCheckType {
		竞技场排名,;

		static protected HeadframeCheckType forNumber(int value) {
			switch (value) {
//			case 1:
//				return 竞技场排名;
			default:
				return 竞技场排名;
			}
		}
	}

	// bean===========================================================================

	private String dataCode;
	private boolean newfound;
	private DateTime expiryDate;

	public String getDataCode() {
		return dataCode;
	}

	public void setDataCode(String dataCode) {
		this.dataCode = dataCode;
	}

	public DateTime getExpiryDate() {
		return expiryDate;
	}

	public void setExpiryDate(DateTime expiryDate) {
		this.expiryDate = expiryDate;
	}

	public boolean isNewfound() {
		return newfound;
	}

	public void setNewfound(boolean newfound) {
		this.newfound = newfound;
	}
	// func==========================================================================

	static public void check(String playerCode, HeadframeCheckType type, int... params) {
		Map<String, headframe> datas = DataCenter.getAllData(headframe.class);
		String lockKey = lockKey(playerCode);
		try {
			if (tryLock(lockKey)) {
				for (headframe data : datas.values()) {
					HeadframeCheckType checkType = HeadframeCheckType.forNumber(data.getConditiontype());
					if (!checkType.equals(type)) {
						continue;
					}
					switch (checkType) {
					case 竞技场排名:
						if (params[0] != data.getConditionnum()) {
							continue;
						}
						break;
					default:
						continue;
					}
					HeadframeModule module = HeadframeModule.get(playerCode, data.getCode());
					if (module == null) {
						module = new HeadframeModule();
						module.setDataCode(data.getCode());
						new CurrencyHandle().addCurrency(playerCode, CCReason.竞技场, Currency.create(CurrencyType.CT_头像框, data.getId(), 1));
					}
					module.setNewfound(true);
					if (data.getTerm() > 0) {
						module.setExpiryDate(DateTime.now().plusHours(data.getTerm()));
					} else {
						module.setExpiryDate(null);
					}
					set(playerCode, module);
					new TipHandle().updateTip(playerCode, TipType.主公_头像框切页, true);
				}
			}
		} catch (Exception e) {
		} finally {
			unlock(lockKey);
		}
	}

	private HeadframeModule update(String playerCode) {
		if (getExpiryDate() != null && DateTime.now().isAfter(getExpiryDate())) {
			// 头像框过期
			remove(playerCode, getDataCode());
			PlayerBean playerBean = PlayerBean.get(playerCode);
			try {
				if (playerBean.tryLock()) {
					playerBean.setHeadframe(DefaultHeadFrame);
					playerBean.set();
				}
				new CurrencyHandle().cost(playerCode, null, Currency.create(CurrencyType.CT_头像框, Integer.parseInt(getDataCode()), 1));
			} catch (NumberFormatException | CurrencyNotEnoughException | InterruptedException e) {
			} finally {
				playerBean.unlock();
			}
			return null;
		}
		return this;
	}

	static public Collection<Integer> newfound(String playerCode) {
		ArrayList<Integer> list = new ArrayList<>();
		Collection<HeadframeModule> all = all(playerCode);

		String lockKey = lockKey(playerCode);
		try {
			if (tryLock(lockKey)) {
				for (HeadframeModule module : all) {
					if (module.isNewfound()) {
						list.add(Integer.parseInt(module.getDataCode()));
						module.setNewfound(false);
						set(playerCode, module);
					}
				}
				new TipHandle().updateTip(playerCode, TipType.主公_头像框切页, false);
			}
		} catch (Exception e) {
		} finally {
			unlock(lockKey);
		}

		return list;
	}

	// redis==========================================================================

	static public String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:HEADFRAME", playerCode);
	}

	static private RMap<String, HeadframeModule> touch(String playerCode) throws RedisException {
		return redis().client().getMap(key(playerCode));
	}

	static public Collection<HeadframeModule> all(String playerCode) {
		ArrayList<HeadframeModule> list = new ArrayList<>();
		String lockKey = lockKey(playerCode);
		try {
			if (tryLock(lockKey)) {
				Collection<HeadframeModule> all = touch(playerCode).readAllValues();
				for (HeadframeModule module : all) {
					if (module.update(playerCode) != null) {
						list.add(module);
					}
				}
			}
		} catch (Exception e) {
		} finally {
			unlock(lockKey);
		}
		return list;
	}

	static private HeadframeModule get(String playerCode, String code) {
		try {
			HeadframeModule module = touch(playerCode).get(code);
			return module;
		} catch (Exception e) {
			return null;
		}
	}

	static private void remove(String playerCode, String avatarCode) {
		try {
			touch(playerCode).remove(avatarCode);
		} catch (RedisException e) {
		}
	}

	static private HeadframeModule set(String playerCode, HeadframeModule module) {
		try {
			touch(playerCode).put(module.getDataCode(), module);
		} catch (RedisException e) {
		}
		return module;
	}

	// lock==========================================================================

	static private String lockKey(String playerCode) {
		return String.format("lock:%s", key(playerCode));
	}

}
